//
//  FWTopicFrameModel.m
//  BirdLoveSheep
//
//  Created by wwj on 16/3/21.
//  Copyright © 2016年 wwj. All rights reserved.
//

#import "FWTopicFrameModel.h"
#import "FWTopicModel.h"
#import "FWTopComment.h"
#import "FWUser.h"

@implementation FWTopicFrameModel

- (void)setTopic:(FWTopicModel *)topic {
    _topic = topic;
    
    CGFloat textX = LRMARGIN;
    CGFloat textY = 50;
    CGFloat textW = kScreenW - 2 * textX;
    CGSize textSize = CGSizeMake(textW, MAXFLOAT);
    CGFloat textH = [topic.text boundingRectWithSize:textSize options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName : [UIFont systemFontOfSize:17]} context:nil].size.height;
    
    CGFloat maxY = textY + textH + TBMARGIN;
    
    if (_topic.type != EssenceTypeWord) {
        CGFloat contentX = LRMARGIN;
        CGFloat contentY = maxY;
        CGFloat contentW = kScreenW - 2 * LRMARGIN;
        CGFloat contentH = contentW / _topic.width * _topic.height;
        if (_topic.type == EssenceTypeVideo || _topic.type == EssenceTypeVoice) {
            if (contentH > contentW * 1.3) {
                contentH = contentW;
                _topic.isBigVideoImg = TRUE;
            }
        }else {
            if (contentH > 2 * contentW) {
                contentH = contentW;
                _topic.isBigImg = TRUE;
            }
        }
        maxY += contentH;
        maxY += TBMARGIN;
        _contentFrame = CGRectMake(contentX, contentY, contentW, contentH);
    }
    
    FWTopComment *topComment = _topic.top_cmt.firstObject;
    
    if (topComment) {
        NSString *topCommentStr = [NSString stringWithFormat:@"%@：%@", topComment.user.username, topComment.content];
        CGFloat topCommentH = [topCommentStr boundingRectWithSize:textSize options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName : [UIFont systemFontOfSize:15]} context:nil].size.height;
        _topCommentH = topCommentH + 10;
        maxY += topCommentH + TOPCOMMH + TBMARGIN*2;
    }
    
    _cellHeight = maxY + 40 + TBMARGIN;
}
@end
